#!/bin/sh
##
## Start/stop script for Eddie ... Chris Miles <cmiles@connect.com.au> 980406
##
## $Id$
##

# Source function library.
. /etc/rc.d/init.d/functions


EDDIERUN=/opt/eddie/bin/eddie_wrapper

start() {
    echo -n $"Starting Eddie: "
    if [ -x $EDDIERUN ]; then
	    daemon --user root "$EDDIERUN &"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/eddie
	    echo
	    return $RETVAL
    else
	    echo "Error: $EDDIERUN not found in $EDDIEBASE/bin"
    fi
}

stop() {
    if test "x`pidof -x eddie.py`" != x; then
	echo -n $"Stopping Eddie: "
	killproc eddie.py
	echo
    else
        echo "Eddie not running"
    fi

    sleep 1

    if test "x`pidof -x eddie_wrapper`" != x; then
        echo -n $"Stopping eddie_wrapper: "
	killproc eddie_wrapper
	echo
    else
        echo "eddie_wrapper not running"
    fi

    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/eddie
    return $RETVAL
}

restart() {
    # kill eddie and rely on eddie_wrapper to restart it
    echo -n $"Restarting Eddie: "
    if test "x`pidof -x eddie.py`" != x; then
	killproc eddie.py
	echo
    else
        echo "Eddie not running"
    fi

    if test "x`pidof -x eddie_wrapper`" = x; then
        echo "eddie_wrapper not running - performing full startup"
	start
    fi
}

case "$1" in
        start)
            start
            ;;
        
        stop)
            stop
            ;;
        
        status)
            status eddie.py
            status eddie_wrapper
            ;;
        restart)
            restart
            ;;
        condrestart)
            if test "x`pidof -x eddie_wrapper`" != x; then
                restart
            fi
            ;;
        
        *)
            echo $"Usage: $0 {start|stop|restart|condrestart|status}"
            exit 1

esac

exit 0
